from handler.api.article import api_article
from handler.api.comment import api_comment
from handler.api.contact import api_contact
from handler.api.index import api_index
from handler.api.login import api_login
from handler.api.upload import api_upload
from handler.job.index import job_index
from handler.view.admin.article_manage import admin_article
from handler.view.admin.comment_manage import admin_comment
from handler.view.admin.friend_link_manage import admin_friendLink
from handler.view.admin.index import admin_index
from handler.view.common.login import view_login
from handler.view.public.about import view_about
from handler.view.public.archive import view_archive
from handler.view.public.article import view_article
from handler.view.public.contact import view_contact
from handler.view.public.index import index_view
from handler.view.public.search import view_search
from settings import Setting
from util.FileUtil import read_yaml_file
from util.WebUtil import get_website_style, get_server_token, get_server_secret, get_server_upload, \
    get_oss_access_secret, get_oss_end_point, get_oss_access_key, get_oss_bucket_name, get_email, get_auth_code, \
    get_qq_app_id, get_qq_app_key, get_qq_callback, get_qq_url, get_qq_token_expires, get_deploy_branch


def init_register_blue_point(app):
    app.register_blueprint(index_view, url_prefix=index_view.url_prefix)
    app.register_blueprint(admin_index, url_prefix=admin_index.url_prefix)
    app.register_blueprint(job_index, url_prefix=job_index.url_prefix)
    app.register_blueprint(api_article, url_prefix=api_article.url_prefix)
    app.register_blueprint(view_article, url_prefix=view_article.url_prefix)
    app.register_blueprint(view_contact, url_prefix=view_contact.url_prefix)
    app.register_blueprint(view_search, url_prefix=view_search.url_prefix)
    app.register_blueprint(view_archive, url_prefix=view_archive.url_prefix)
    app.register_blueprint(view_about, url_prefix=view_about.url_prefix)
    app.register_blueprint(api_index, url_prefix=api_index.url_prefix)
    app.register_blueprint(api_upload, url_prefix=api_upload.url_prefix)
    app.register_blueprint(admin_article, url_prefix=admin_article.url_prefix)
    app.register_blueprint(view_login, url_prefix=view_login.url_prefix)
    app.register_blueprint(api_login, url_prefix=api_login.url_prefix)
    app.register_blueprint(api_contact, url_prefix=api_contact.url_prefix)
    app.register_blueprint(api_comment, url_prefix=api_comment.url_prefix)
    app.register_blueprint(admin_comment, url_prefix=admin_comment.url_prefix)
    app.register_blueprint(admin_friendLink, url_prefix=admin_friendLink.url_prefix)


def init_website_app_config(app):
    path = "{}/app.yaml".format(app.root_path)
    app_yaml = read_yaml_file(path)
    db_user = app_yaml['website']['database']['username']
    db_pass = app_yaml['website']['database']['password']
    db_name = app_yaml['website']['database']['dbname']
    db_url = app_yaml['website']['database']['url']
    db_port = app_yaml['website']['database']['port']
    db_uri = "mysql+pymysql://{}:{}@{}:{}/{}".format(db_user, db_pass, db_url, db_port, db_name)
    app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
    app.config["SQLALCHEMY_POOL_RECYCLE "] = 10
    app.config["SQLALCHEMY_POOL_SIZE  "] = 30
    app.config["SQLALCHEMY_POOL_TIMEOUT  "] = 3
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
    Setting.style = get_website_style(app)
    Setting.app_path = app.root_path
    Setting.token = get_server_token(app)
    Setting.branch = get_deploy_branch(app)
    Setting.secret = get_server_secret(app)
    Setting.upload_path = get_server_upload(app)
    Setting.oss_access_secret = get_oss_access_secret(app)
    Setting.oss_end_point = get_oss_end_point(app)
    Setting.oss_access_key = get_oss_access_key(app)
    Setting.oss_bucket_name = get_oss_bucket_name(app)
    Setting.email = get_email(app)
    Setting.auth_code = get_auth_code(app)
    Setting.qq_appId = get_qq_app_id(app)
    Setting.qq_appKey = get_qq_app_key(app)
    Setting.qq_callBack = get_qq_callback(app)
    Setting.qq_url = get_qq_url(app)
    Setting.qq_token_expires = get_qq_token_expires(app)
